package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.service.InstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @program: tvucc-aggregator
 * @description: InstanceController
 * @author lebronchen
 * @create: 2019-12-16 10:56
 **/
@RestController
@RequestMapping("/instance")
public class InstanceController {

    @Autowired
    private InstanceService instanceService;

    /**
     *
     * @return Result
     * @throws IOException 编译异常
     */
    @GetMapping("/isInstanceExist")
    public Result isInstanceExist() throws IOException {
        boolean exist = instanceService.isTestInstanceExist();
        return ResultUtil.success(exist);
    }

    /**
     *
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/openInstance")
    public Result openInstance() throws IOException {
        int result = instanceService.openTestInstance();
        if (result == 1) {
            return ResultUtil.error("81001701", "Instance running");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/shutdownInstance")
    public Result shutdownInstance() throws IOException {
        int result = instanceService.shutdownTestInstance();
        if (result == 1) {
            return ResultUtil.error("81001801", "No instance running");
        }
        return ResultUtil.success();
    }

}
